#include "Identifier.h"
#include "CharInfo.h"
#include "Exception.hpp"


namespace cal
{

bool is_identifier(const std::string& str)
{
	if (str.empty()) return false;
	if (!is_identifier_start(str[0])) return false;
	int size = str.size();
	for (int i = 1; i < size; ++i) 
		if (!is_identifier_continue(str[i])) return false;
	return true;
}

Identifier::Identifier(const std::string& str)
	: _val{ str }
{
	if (!is_identifier(_val)) throw Exception{"illegal identifier: " + str};
}


std::istream& operator>>(std::istream& is, Identifier& id) 
{
	if (!is_identifier_start(is.peek())) {
		id._val = "";
		return is;
	}
	id._val.push_back(is.get());
	while (is_identifier_continue(is.peek())) 
		id._val.push_back(is.get());
	return is;
}

std::ostream& operator<<(std::ostream& os, const Identifier& id)
{
	os << id.str();
	return os;
}

}   // namespace cal
